<?php
include('config.php');
header("content-type:text/html; charset=utf-8");
$where = "1";

$type_name = isset($_POST['type_name']) ? $_POST['type_name'] : '';
$name = isset($_POST['name']) ? $_POST['name'] : '';
$number_name = isset($_POST['number_name']) ? $_POST['number_name'] : '';
$warning_number = isset($_POST['warning_number']) ? $_POST['warning_number'] : '';

if($type_name){
	$type_id_rs = $mysql->selectlist("type","type_name='{$type_name}'");
	$type_id = $type_id_rs[0]['type_id'];
	$where = "s.type_id = '{$type_id}'";
}
if(($type_name&&$name) || $name){
	$breed_id_rs = $mysql->selectlist("breed","breed_name='{$name}'");
	$breed_id = $breed_id_rs[0]['breed_id'];
	$where = "s.breed_id = '{$breed_id}'";
}
if(($type_name&&$name&&$number_name) || $number_name){
	$number_id_rs = $mysql->selectlist("number","number_name='{$number_name}'");
	$number_id = $number_id_rs[0]['number_id'];
	$where = "s.number_id = '{$number_id}'";
}
if($type_name && $name && $number_name && $warning_number){
	$where = "s.number_id = '{$number_id}' and s.stock_num<='{$warning_number}'";
}
if($warning_number){
	$where = "s.stock_num<='{$warning_number}'";
}

$result = $mysql->selectList('caution_line');
$caution_line = @$result[0]['caution_line'];

$sql = "SELECT s.*,n.number_name,b.breed_name,t.type_name FROM stock s left join `number` n on s.number_id=n.number_id left join breed b on s.breed_id=b.breed_id left join type t on s.type_id=t.type_id";
$sql .= " WHERE ".$where." ORDER BY s.stock_id DESC LIMIT 0,20";
$n = 0;
echo "<div class='sw_info' id='sw_info'>";
echo "<table id='tables'>";
echo "<tr class='sw_top_tr'>";
echo "<th>类别</th><th>品种</th><th>编号</th><th>数量</th><th>进货数量</th><th>售出数量</th><th>剩余数量</th>";
$rs = $mysql->execute($sql);
while($row = mysql_fetch_assoc($rs)){
	$n = $n;
	$num = $n%2;
	if($row['stock_num']<=$caution_line){
		$color = "red";
	}else{
		$color = "blue";
	}
	echo "<tr class='sw_tr{$num}'><td>{$row['type_name']}</td><td>{$row['breed_name']}</td><td>{$row['number_name']}</td><td>{$row['stock_num']}</td><td>{$row['stock_goods_num']}</td><td>{$row['buy_goods_num']}</td><td><span style='color:{$color};'>{$row['stock_num']}</span></td></tr>";
	$n++;
}
echo "</tr>";
echo "</table>";
echo "</div>";